package com.example.practice2023.controller;

import org.springframework.web.bind.annotation.*;
import com.example.practice2023.service.ArticleService;
import com.example.practice2023.pojo.Article;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/article")
@Api(value = "用户数据test",tags = "用户数据接口API")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @RequestMapping(value = "/selectByPage",method = RequestMethod.GET)
    public Map<String, Object> selectByPage(int page, int limit,Article article) {
        return articleService.selectByPage(page, limit,article);
    }

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Map<String, Object> insert(@RequestBody Article article) {
        return articleService.insert(article);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public Map<String, Object> deleteById(@RequestParam Integer article_ID) {
        return articleService.deleteById(article_ID);
    }
    @RequestMapping(value = "/updateById",method = RequestMethod.POST)
    public Map<String, Object> updateById(@RequestBody Article article) {
        return articleService.updateById(article);
    }

    @RequestMapping(value = "/setStatus",method = RequestMethod.POST)
    public Map<String, Object> setStatus(@RequestBody Article article) {
        return articleService.setStatus(article);
    }


    @RequestMapping(value="upload",method= RequestMethod.POST,produces="application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> uploadResource(HttpServletRequest request, HttpServletResponse response, HttpSession session, MultipartFile file) throws Exception{
        //上传路径保存设置
//        String path = request.getServletContext().getRealPath("src/main/resources/static/images");
//        String path = "E:\\ideaImg\\"+new Date().getTime()+file.getOriginalFilename();
        String path = "C:\\Users\\34059\\IdeaProjects\\practice2023\\src\\main\\resources\\static\\images";
        System.out.println(path);
        //获取上传的文件名
        /*String fileName = file.getOriginalFilename();
        System.out.println(fileName);*/
        //这里设置每个用户头像img只有一个
        String fileName=new Date().getTime()+file.getOriginalFilename();
        File dir = new File(path,fileName);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //MultipartFile自带的解析方法
        file.transferTo(dir);
        //upload要求返回的数据格式
        Map<String, Object> uploadData = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        uploadData.put("code", "0");
        uploadData.put("msg", "ok");
        uploadData.put("name", fileName);
        //将文件路径返回
        data.put("src", dir.getPath());
        uploadData.put("data", data);
        return uploadData;
    }

}
